/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.common;

import cn.ac.iie.di.wx.configuration.Configuration;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String MSG_URL = "msg_url";
    public static final String USER_URL = "user_url";
    public static final String USER = "user";
    public static final String PASSWORD = "password";
    public static final String REDIS_MASTER_NAME = "redis.master.name";
    public static final String REDIS_HOSTS = "redis.hosts";
    public static final String REDIS_DB_NAME = "redis.db.name";
    public static final String MASSALL = "massAll";
    public static final String MASSSUC = "massSuc";
    public static final String SINGLEALL = "singleAll";
    public static final String SINGLESUC = "singleSuc";
    public static final String DATASIZE = "dataSize";
    public static final String ALLCOUNT = "allCount";
    public static final String SUCCESSCOUNT = "successCount";
    public static final String EVALUATE = "evaluate";

    public static final String yesterdayDataSize = "yesterdayDataSize";
    public static final String theDayBeforeDataSize = "theDayBeforeDataSize";
    public static final String allCount = "AllCount";
    public static final String successCount = "SuccessCount";
    public static final String DATE = "date";

    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    static Logger logger = Logger.getLogger(RuntimeEnv.class.getName());

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            logger.error("configuration object is null");
            throw new Exception("configuration object is null");
        }

        conf = pConf;

        setKeyAndValue(MSG_URL, "msg_url");
        setKeyAndValue(USER_URL, "user_url");
        setKeyAndValue(USER, "user");
        setKeyAndValue(PASSWORD, "password");
        setKeyAndValue(REDIS_MASTER_NAME, "redis_master_name");
        setKeyAndValue(REDIS_HOSTS, "redis_hosts");
        setKeyAndValue(REDIS_DB_NAME, MSG_URL);
        setKeyAndValue(MASSALL, "massAll");
        setKeyAndValue(MASSSUC, "massSuc");
        setKeyAndValue(SINGLEALL, "singleAll");
        setKeyAndValue(SINGLESUC, "singleSuc");
        setKeyAndValue(DATASIZE, "dataSize");
        setKeyAndValue(ALLCOUNT, "allCount");
        setKeyAndValue(SUCCESSCOUNT, "successCount");
        setKeyAndValue(EVALUATE, "evaluate");
        setKeyAndValue(DATE, "date");
        setKeyAndValue(yesterdayDataSize, "yesterdayDataSize");
        setKeyAndValue(theDayBeforeDataSize, "theDayBeforeDataSize");
        setKeyAndValue(allCount, "AllCount");
        setKeyAndValue(successCount, "SuccessCount");
        return true;
    }

    public static boolean setKeyAndValue(String KEY, String parameter) throws Exception {
        String value = conf.getString(KEY, "");
        if (value.isEmpty()) {
            logger.error("parameter " + parameter + " does not exist or is not defined");
            throw new Exception("parameter " + parameter + " does not exist or is not defined");
        }
        addParam(KEY, value);
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
